package online.indigo6a.eshop.model;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.Data;
import online.indigo6a.eshop.exception.CatalogDomainException;

import java.math.BigDecimal;

@Data
@Entity(name="Catalog")
public class CatalogItem {
    @Id
    private Long id;
    private String name;
    private String description;
    private BigDecimal price;
    private String pictureFileName;
    private int catalogTypeId;
    private int catalogBrandId;
    private int availableStock;
    private int maxStockThreshold;
    private boolean onReorder;
    public int removeStock(int quantityDesired) {
        if(availableStock==0) {
            throw new CatalogDomainException("Empty stock");
        }
        if(quantityDesired <=0 ) {
            throw new CatalogDomainException("item units should be greater than zero");
        }
        int removed = Math.min(quantityDesired,availableStock);
        this.availableStock -= removed;
        return removed;
    }
    public int addStock(int quantity) {
        int origin = this.availableStock;
        if(this.availableStock+quantity>this.maxStockThreshold) {
            this.availableStock +=(this.maxStockThreshold-this.availableStock);
        }
        else {
            this.availableStock += quantity;
        }
        this.onReorder = false;
        return this.availableStock-origin;
    }

}
